package com.iwhalecloud.bss.kite.cucc.service.cust;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.hsf.baseinf.service.InfExchangeService;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.cust.IVerifyCustService;
import com.iwhalecloud.bss.kite.cucc.common.callclient.HttpsCallClient;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalCallClientEnum;
import com.iwhalecloud.bss.kite.cucc.service.inf.InfServiceHelper;
import com.iwhalecloud.bss.kite.cucc.service.util.LocalTokenUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class VerifyCustService implements IVerifyCustService {

    @Value("${interface.call.mode:hsf}")
    private String mode;

    private static final String VERIFY_CUST_OP_CODE = "json_chinaUnicom_govEnterprise_5GmessageOMplatform_verifyCust";

    @Override
    public Map<String, Object> qryCustAttributes(String custName, String provinceCode) {
        //请求参数
        Map<String, Object> verifyCustReq = new HashMap<>();
        verifyCustReq.put("CUST_NAME", custName);
        verifyCustReq.put("PROVINCE_CODE", provinceCode);
        Map<String, Object> uniBssBody = new HashMap<>();
        uniBssBody.put("VERIFY_CUST_REQ", verifyCustReq);
        Map<String, Object> param = new HashMap<>();
        param.put("UNI_BSS_BODY", uniBssBody);
        param.put("OP_CODE", VERIFY_CUST_OP_CODE);
        InfServiceHelper.adaptGray(param, verifyCustReq);
        String resultStr;
        if (KiteStringUtils.isEqual(mode, "http")) {
            resultStr = HttpsCallClient.postStringWithObject(LocalCallClientEnum.INF_COMMON_URL, param, LocalTokenUtil.getHeaderMap());
        } else {
            InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
            resultStr = infExchangeService.exchange(new JSONObject(param));
        }
        //返回内容
        Map<String, Object> verifyCustRsp = new HashMap();
        if (KiteStringUtils.isNotEmpty(resultStr)) {
            Map<String, Object> resultMap = JSON.parseObject(resultStr, Map.class);
            Map<String, Object> bodyMap = KiteMapUtils.getValue(resultMap, "UNI_BSS_BODY");
            if (KiteMapUtils.isNotEmpty(bodyMap) && bodyMap.containsKey("VERIFY_CUST_RSP")){
                verifyCustRsp = KiteMapUtils.getValue(bodyMap, "VERIFY_CUST_RSP");
            }
        }
        return verifyCustRsp;
    }
}
